chartView.tsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. 'use client'
  2. import React, { useState } from 'react'
  3. import dayjs from 'dayjs'
  4. import quarterOfYear from 'dayjs/plugin/quarterOfYear'
  5. import { useTranslation } from 'react-i18next'
  6. import type { PeriodParams } from '@/app/components/app/overview/appChart'
  7. import { AvgResponseTime, AvgSessionInteractions, AvgUserInteractions, ConversationsChart, CostChart, EndUsersChart, MessagesChart, TokenPerSecond, UserSatisfactionRate, WorkflowCostChart, WorkflowDailyTerminalsChart, WorkflowMessagesChart } from '@/app/components/app/overview/appChart'
  8. import type { Item } from '@/app/components/base/select'
  9. import { SimpleSelect } from '@/app/components/base/select'
  10. import { TIME_PERIOD_LIST } from '@/app/components/app/log/filter'
  11. import { useStore as useAppStore } from '@/app/components/app/store'
  12. dayjs.extend(quarterOfYear)
  13. const today = dayjs()
  14. const queryDateFormat = 'YYYY-MM-DD HH:mm'
  15. export type IChartViewProps = {
  16. appId: string
  17. }
  18. export default function ChartView({ appId }: IChartViewProps) {
  19. const { t } = useTranslation()
  20. const appDetail = useAppStore(state => state.appDetail)
  21. const isChatApp = appDetail?.mode !== 'completion' && appDetail?.mode !== 'workflow'
  22. const isWorkflow = appDetail?.mode === 'workflow'
  23. const [period, setPeriod] = useState<PeriodParams>({ name: t('appLog.filter.period.last7days'), query: { start: today.subtract(7, 'day').startOf('day').format(queryDateFormat), end: today.endOf('day').format(queryDateFormat) } })
  24. const onSelect = (item: Item) => {
  25. if (item.value === 'all') {
  26. setPeriod({ name: item.name, query: undefined })
  27. }
  28. else if (item.value === 0) {
  29. const startOfToday = today.startOf('day').format(queryDateFormat)
  30. const endOfToday = today.endOf('day').format(queryDateFormat)
  31. setPeriod({ name: item.name, query: { start: startOfToday, end: endOfToday } })
  32. }
  33. else {
  34. setPeriod({ name: item.name, query: { start: today.subtract(item.value as number, 'day').startOf('day').format(queryDateFormat), end: today.endOf('day').format(queryDateFormat) } })
  35. }
  36. }
  37. if (!appDetail)
  38. return null
  39. return (
  40. <div>
  41. <div className='flex flex-row items-center mt-8 mb-4 text-gray-900 text-base'>
  42. <span className='mr-3'>{t('appOverview.analysis.title')}</span>
  43. <SimpleSelect
  44. items={TIME_PERIOD_LIST.map(item => ({ value: item.value, name: t(`appLog.filter.period.${item.name}`) }))}
  45. className='mt-0 !w-40'
  46. onSelect={onSelect}
  47. defaultValue={7}
  48. />
  49. </div>
  50. {!isWorkflow && (
  51. <div className='grid gap-6 grid-cols-1 xl:grid-cols-2 w-full mb-6'>
  52. <ConversationsChart period={period} id={appId} />
  53. <EndUsersChart period={period} id={appId} />
  54. </div>
  55. )}
  56. {!isWorkflow && (
  57. <div className='grid gap-6 grid-cols-1 xl:grid-cols-2 w-full mb-6'>
  58. {isChatApp
  59. ? (
  60. <AvgSessionInteractions period={period} id={appId} />
  61. )
  62. : (
  63. <AvgResponseTime period={period} id={appId} />
  64. )}
  65. <TokenPerSecond period={period} id={appId} />
  66. </div>
  67. )}
  68. {!isWorkflow && (
  69. <div className='grid gap-6 grid-cols-1 xl:grid-cols-2 w-full mb-6'>
  70. <UserSatisfactionRate period={period} id={appId} />
  71. <CostChart period={period} id={appId} />
  72. </div>
  73. )}
  74. {!isWorkflow && isChatApp && (
  75. <div className='grid gap-6 grid-cols-1 xl:grid-cols-2 w-full mb-6'>
  76. <MessagesChart period={period} id={appId} />
  77. </div>
  78. )}
  79. {isWorkflow && (
  80. <div className='grid gap-6 grid-cols-1 xl:grid-cols-2 w-full mb-6'>
  81. <WorkflowMessagesChart period={period} id={appId} />
  82. <WorkflowDailyTerminalsChart period={period} id={appId} />
  83. </div>
  84. )}
  85. {isWorkflow && (
  86. <div className='grid gap-6 grid-cols-1 xl:grid-cols-2 w-full mb-6'>
  87. <WorkflowCostChart period={period} id={appId} />
  88. <AvgUserInteractions period={period} id={appId} />
  89. </div>
  90. )}
  91. </div>
  92. )
  93. }